import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;

public class StudentRecord extends JFrame implements ActionListener{
        private JLabel lbl1 = new JLabel("ID Number: ");
        private JTextField txt1 = new JTextField();
        private JLabel lbl2 = new JLabel("Password: ");
        private JTextField txt2 = new JTextField();
        private JButton btn1 = new JButton("Save");
        private JButton btn2 = new JButton("Clear Fields");
        private JButton btn3 = new JButton("Back");
        String line;
    String username = "";
    JFrame frame;


public StudentRecord(){
        frame = new JFrame("Add Student");
        frame.setBounds(400,150,300,300);
        frame.setLayout(new GridLayout(4,1));
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(1,1));
        panel.add(lbl1);
        panel.add(txt1);

        JPanel panel1 = new JPanel();
        panel1.setLayout(new GridLayout(1,1));
        panel1.add(lbl2);
        panel1.add(txt2);

        JPanel panel2 = new JPanel();
        panel2.setLayout(new GridLayout(1,1));
        panel2.add(btn1);
        panel2.add(btn2);
        btn1.addActionListener(this);
        btn2.addActionListener(this);

        JPanel panel3 = new JPanel();
        panel3.setLayout(new GridLayout(1,1));
        panel3.add(btn3);
        btn3.addActionListener(this);

                frame.add(panel);
                frame.add(panel1);
                frame.add(panel2);
                frame.add(panel3);
}
public void AddRecord(){
        ArrayList list = new ArrayList();
        try{
                BufferedReader reader = new BufferedReader(new FileReader("student.txt"));
                String tmp;
        while ((tmp = reader.readLine()) != null)
                list.add(tmp);
                reader.close();
                        list.add(txt1.getText()+ ',' + txt2.getText());
                        BufferedWriter writer = new BufferedWriter(new FileWriter("student.txt"));
                for (int i = 0; i < list.size(); i++)
                        writer.write(list.get(i) + "\r\n");
                                        writer.close();
                                        JOptionPane.showMessageDialog(null,"New Student Added:"+ txt1.getText());

        }catch(Exception e){

        }

}
public void actionPerformed(ActionEvent e){
        Object src = e.getSource();
        String t1 = txt1.getText();
        String t2 = txt2.getText();
        //Jframe f = new Jframe();

        if(src == btn1){
                if (true){

            try {
            BufferedReader br = new BufferedReader(new FileReader("student.txt"));
            while ( ( line = br.readLine() ) != null ) {
                String [] tmp = line.split(",");
                username = tmp[0];
                if(t1.trim().isEmpty() || t2.trim().isEmpty()) {
                        JOptionPane.showMessageDialog(null,"Cannot add without complete data!");
                        }
                        if(t1.trim().equalsIgnoreCase(username)){
                            JOptionPane.showMessageDialog(null,"Username already exist!");
                            return;
                    }
            }

        } catch (Exception err) {
                err.printStackTrace();
        }
                AddRecord();
        txt1.setText("");
                txt2.setText("");
                return;
    }
        }else if (src == btn2){
                txt1.setText("");
                txt2.setText("");
        }else if(src == btn3){
        StudentMainClass studProg=new StudentMainClass();
        try {
                studProg.run();
                setVisible(false);
                studProg.setVisible(false);
                frame.dispose();
                studProg.setSize(800,600);
        } catch (Exception err) {
                err.printStackTrace();
    }
        }
}

}